package bar.dark.lee.stats;

import java.util.ArrayList;
import java.util.Calendar;

import bar.dark.lee.BDConstents;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.select.SelectActivity;
import bar.dark.lee.setting.TipInfoActivity;
import bar.dark.lee.setting.TipsActivity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class StatsMonthListActivity extends SuperActivity
{
	private ListView listView;
	
	private ArrayList<StatsMonthData> list;
	
	private int inoutFlag;
	
	private int year;
	private int month;
	
	private int startDate;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setFindView();
		new ShowDialog().execute(0);
	}

	private void setFindView()
	{
		setContentView(R.layout.stats_month_list_activity);
		
		listView = (ListView) findViewById(R.id.statsMonthListview);
	}
	
	private void setData()
	{
		Calendar cal = Calendar.getInstance();
		year = cal.get(Calendar.YEAR);
		month = cal.get(Calendar.MONTH);
		
		Intent intent = getIntent();
		
		inoutFlag = intent.getIntExtra(StatsActivity.STATS_MONTH_FLAG, -1);
		list = intent.getParcelableArrayListExtra(StatsMonthActivity.STATS_MONTH_DATA);
		year = intent.getIntExtra(StatsActivity.SELECT_YEAR, year);
		month = intent.getIntExtra(StatsActivity.SELECT_MONTH, month);
		
		// 달 시작일 가져온다
    	SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
    	startDate = Integer.parseInt(pref.getString(BDConstents.START_DATE, "1"));
	}

	private void setStatsData(int inoutFlag)
	{
		if (inoutFlag == StatsActivity.STATS_IN)
		{
			setTitle("수입 월별 리스트");
		}
		else if (inoutFlag == StatsActivity.STATS_OUT)
		{
			setTitle("지출 월별 리스트");
		}
		else if (inoutFlag == StatsActivity.STATS_BALANCE)
		{
			setTitle("잔액 월별 리스트");
		}
		
		if (list.size() <= 0)
		{
			View nodataView = getLayoutInflater().inflate(R.layout.view_no_result, null);
			listView.addHeaderView(nodataView, null, false);
			listView.setHeaderDividersEnabled(false);
		}
		
		StatsMonthAdapter adapter = new StatsMonthAdapter(this, R.layout.list_item_stats_month, list);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(adapter);
	}
	
	private class StatsMonthAdapter extends ArrayAdapter<StatsMonthData> implements OnItemClickListener
	{
		public StatsMonthAdapter(Context context, int textViewResourceId,
				ArrayList<StatsMonthData> list)
		{
			super(context, textViewResourceId, list);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null)
			{
				v = getLayoutInflater().inflate(R.layout.list_item_stats_month, null);
			}
			
			StatsMonthData data = list.get(position);
			
			TextView name = (TextView) v.findViewById(R.id.listStatsMonthName);
			TextView money = (TextView) v.findViewById(R.id.listStatsMonthMoney);
			
			if (startDate == 1)
			{
				name.setText(data.getMonth());

			}
			else
			{
				try
				{
					String month = "";
					int intMonth = 0;
					month = data.getMonth().substring(0, 2).trim();
					intMonth = Integer.parseInt(month);
					
					if (intMonth == 12)
					{
						name.setText(month + "~" + "1 월");
					}
					else
					{
						name.setText(month + "~" + (intMonth+1) + " 월");
					}
					
					//name.setText(data.getMonth().charAt(0) + "~" + (Integer.parseInt(data.getMonth().charAt(0)+"")+1) + " 월");
				}
				catch (Exception e)
				{
					name.setText(data.getMonth());
				}
			}
			
			money.setText(BDUtil.getWon(data.getMoney()) + shape);
			
			return v;
		}
		
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
				long arg3) {
			Intent intent = new Intent(StatsMonthListActivity.this, SelectActivity.class);
			intent.putExtra(StatsActivity.STATS_MONTH_FLAG, inoutFlag);
			intent.putExtra(StatsActivity.SELECT_YEAR , year);
			intent.putExtra(StatsActivity.SELECT_MONTH, pos);
			startActivity(intent);
		}
	}
	
	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(StatsMonthListActivity.this);
			dialog.setCancelable(false);
			dialog.setMessage("Loading...");
			dialog.show();
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			
			if (arg0[0] == 0)
			{
				setData();
				return 0;
			}
			
			return null;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			
			dialog.dismiss();
			
			if (result == 0)
			{
				setStatsData(inoutFlag);
			}
		}
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(this, TipInfoActivity.class);
		intent.putExtra(TipsActivity.TIP_REQUEST_KEY, TipsActivity.TIP_STATS);
		startActivity(intent);
	}
}
